from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QIcon, QPalette, QBrush, QPixmap, QFont
from PyQt5.QtWidgets import QApplication, QDialog, QHBoxLayout, QVBoxLayout
import sys
import math
import matplotlib
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg
from matplotlib.backends.backend_template import FigureCanvas
from matplotlib.figure import Figure
import numpy as np
from PIL import Image
import os
import matplotlib.pyplot as plt
from pylab import mpl

mpl.rcParams['axes.unicode_minus'] = False  # 正常显示负号


class locationUi_2(QDialog):
    signal_write_fusion_msg = QtCore.pyqtSignal(str)  # 数据融合写信息
    def __init__(self, radar_fusion_time,laser_fusion_time,left_corner_x = -5.8,left_corner_y = 50.8):
        super(locationUi_2, self).__init__()
        self.corner_x = left_corner_x
        self.corner_y = left_corner_y

        self._translate = QtCore.QCoreApplication.translate
        self.setObjectName("TCP-UDP")
        self.setAcceptDrops(False)
        self.setSizeGripEnabled(False)
        font = QtGui.QFont()
        font.setFamily("Yuppy TC")
        font.setPointSize(8)
        self.setFont(font)

        self.label_time_registration = QtWidgets.QLabel()  #
        self.textBrowser_time_registration  = QtWidgets.QTextBrowser()  # 文本浏览框

        self.label_place_registration = QtWidgets.QLabel()  #

        self.label_location_laser_pre = QtWidgets.QLabel()  #
        self.lineEdit_location_laser_pre  = QtWidgets.QLineEdit()  #
        self.lineEdit_location_laser_pre .setFixedWidth(150)

        self.pushButton_turn_location = QtWidgets.QPushButton()  #

        self.label_location_laser = QtWidgets.QLabel()  #
        self.lineEdit_location_laser = QtWidgets.QLineEdit()  #
        self.lineEdit_location_laser.setFixedWidth(150)

        self.label_location_radar = QtWidgets.QLabel()  #
        self.lineEdit_location_radar = QtWidgets.QLineEdit()  #
        self.lineEdit_location_radar.setFixedWidth(150)

        self.pushButton_error_location = QtWidgets.QPushButton()  #
        self.textBrowser_place_registration = QtWidgets.QTextBrowser()  # 文本浏览框

        self.h_box_1 = QHBoxLayout()
        self.h_box_2 = QHBoxLayout()
        self.h_box_3 = QHBoxLayout()
        self.v_box_left = QVBoxLayout()  # 左侧布局
        self.v_box_right = QVBoxLayout()  # 左侧布局
        self.h_box_all = QHBoxLayout()  #

        self.layout_ui()
        self.ui_translate()
        self.write_time_registration_msg(radar_fusion_time + '\n' + laser_fusion_time)
        self.connect()

    def layout_ui(self):

        self.v_box_left.addWidget(self.label_time_registration, alignment=QtCore.Qt.AlignHCenter)
        self.v_box_left.addWidget(self.textBrowser_time_registration)

        self.h_box_1.addWidget(self.label_location_laser_pre)
        self.h_box_1.addWidget(self.lineEdit_location_laser_pre)
        self.h_box_1.addWidget(self.pushButton_turn_location)

        self.h_box_2.addWidget(self.label_location_laser)
        self.h_box_2.addStretch(1)  # 增加伸缩量
        self.h_box_2.addWidget(self.lineEdit_location_laser)
        self.h_box_2.addStretch(8)  # 增加伸缩量

        self.h_box_3.addWidget(self.label_location_radar)
        self.h_box_3.addWidget(self.lineEdit_location_radar)
        self.h_box_3.addWidget(self.pushButton_error_location)

        self.v_box_right.addWidget(self.label_place_registration, alignment=QtCore.Qt.AlignHCenter)
        self.v_box_right.addLayout(self.h_box_1)
        self.v_box_right.addLayout(self.h_box_2)
        self.v_box_right.addLayout(self.h_box_3)
        self.v_box_right.addWidget(self.textBrowser_place_registration)

        self.h_box_all.addLayout(self.v_box_left)
        self.h_box_all.addLayout(self.v_box_right)

        self.setLayout(self.h_box_all)

    def ui_translate(self):
        self.setWindowTitle(self._translate("TCP-UDP", "配准"))
        self.label_time_registration.setText(self._translate("TCP-UDP", "时间配准"))

        self.label_place_registration.setText(self._translate("TCP-UDP", "空间配准"))
        self.label_location_laser.setText(self._translate("TCP-UDP", "激光坐标"))
        self.label_location_laser_pre.setText(self._translate("TCP-UDP", "激光坐标前"))
        self.label_location_radar.setText(self._translate("TCP-UDP", "雷达坐标"))
        self.pushButton_error_location.setText(self._translate("TCP-UDP", "计算误差"))
        self.pushButton_turn_location.setText(self._translate("TCP-UDP", "转换"))

    def connect(self):
        self.pushButton_turn_location.clicked.connect(self.turn_laser)  # 显示表格 按钮
        self.pushButton_error_location.clicked.connect(self.click_error)  # 显示表格 按钮
    def turn_laser(self):
        person_location_laser = self.lineEdit_location_laser_pre.text().split(',')
        laser_x = self.corner_x + (3.51 + float(person_location_laser[1]))*math.cos(math.pi/180)
        laser_y = self.corner_y + (4.16 - float(person_location_laser[0]))*math.cos(math.pi/180)
        self.lineEdit_location_laser.setText(format(laser_x,'.3f')+','+format(laser_y,'.3f'))

    def click_error(self):
        person_location_laser = self.lineEdit_location_laser.text().split(',')
        person_location_radar = self.lineEdit_location_radar.text().split(',')
        error_data = ''
        if person_location_laser != [''] and person_location_laser != ['']:
            error_x = float(person_location_radar[0]) - float(person_location_laser[0])
            error_y = float(person_location_radar[1]) - float(person_location_laser[1])
            error = (error_x ** 2 + error_y ** 2) ** 0.5

            error_data = '总误差为' + format(error, '.3f') + 'm' + '\n'

        self.write_place_registration_msg(error_data )


    def write_time_registration_msg(self, msg):
        self.textBrowser_time_registration.insertPlainText(msg)
        # 滚动条移动到结尾
        self.textBrowser_time_registration.moveCursor(QtGui.QTextCursor.End)

    def write_place_registration_msg(self, msg):
        self.textBrowser_place_registration.insertPlainText(msg)
        # 滚动条移动到结尾
        self.textBrowser_place_registration.moveCursor(QtGui.QTextCursor.End)



if __name__ == '__main__':
    app = QApplication(sys.argv)
    msg = [[(2, 50)]]
    ui = locationUi_2('1','1')
    ui.show()
    sys.exit(app.exec_())
